const express = require('express')
const fs = require('fs')

const config = require(process.cwd() + '/src/config/config')
const Game = require(process.cwd() + '/src/class/index')

let router = express.Router()

const doAction = async (req) => {  
  const options = {
    id: req.body.id,
    ...req.body.settings
  }
  let map = new Game.Map(options)
  await map.init()
  
  // 将数据变更保存地图配置文件
  await map.save()
  
  // 返回新数据
  return map.mapData
}

/* GET users listing. */
router.post('/', function(req, res) {
  doAction(req).then((data) => {
    res.json(data)
  })
  .catch((err) => {
    console.log(err)
    res.sendStatus(500)
  })
})

module.exports = router
